//
// PS: Define the fined rectangle receiver data structure.
//

#ifndef SOLARENERGY_CHIER_REFINED_RECTANGLE_RECEIVER_CUH
#define SOLARENERGY_CHIER_REFINED_RECTANGLE_RECEIVER_CUH

#include "CudaReceiver.cuh"
#include <algorithm/rayTracing/qmcrt_util/global_function.cuh>

namespace solar::cuda
{
    class CudaRefinedRectangleReceiver : public CudaReceiver
    {
        public:
            __device__ __host__ CudaRefinedRectangleReceiver() = default;

            CudaRefinedRectangleReceiver(
                const CudaRefinedRectangleReceiver& refined_rectangle_receiver);

            // TODO: ADD TESTS
            // TODO
            //  ？？？？？？？？？？这里为什么要加offset，radius指的是什么
            __device__ __host__ auto GIntersect(const float3& origin, const float3& dir, float& t,
                                                float& u, float& v) -> bool;

            void CInit(float pixel_per_meter_for_receiver) override;

            auto getFocusCenter(const float3& heliostat_position) -> float3 override;

            void setFocusCenter();

            [[nodiscard]] auto getRectangleVertex(int index) const -> float3;
            [[nodiscard]] auto getLocalNormal() const -> float3;

        private:
            float3 rect_vertexes_[4]; // Four vertexes of rectangle
            float3 local_normal_;
            float3 focus_center_;

            void Cinit_vertex();
            void Cset_local_normal(); // Set local normal
            void Cset_local_vertex(); // Set local vertex
            void Cset_world_vertex(); // Set world vertex
            void Cset_resolution(float pixel_per_meter_for_receiver) override;
            __device__ __host__ auto helper(const float3& origin, const float3& dir,
                                            const float3& pos, float& t, float& u, float& v,
                                            float radius, float base, bool isInRight) -> bool;
    };
} // namespace solar::cuda

#if defined(SOLAR_CUDA_STANDALONE_COMPILE) && !defined(SOLAR_BUILD_CUDA_REFINED_RECTANGLE_RECEIVER)
    #include "CudaRefinedRectangleReceiver.cu"
#endif

#endif // SOLARENERGY_CHIER_RECTANGLE_RECEIVER_CUH
